package com.ts.enterprise.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis 工具类
 *
 * @author
 */
@Slf4j
public class RedisUtil {

    /**
     * 设置缓存
     *
      *
      * Description: 
      *
      * @param:
      * @return: 
      * @auther: 
      * @date: 2021/7/15 11:19
      */

    public static void set(String key, String value) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置缓存，并设置过期时间
     *
     * @param key
     * @param value
     * @param timeout
     */
    public static void set(String key, String value, long timeout) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        stringRedisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     */
    public static boolean hasKey(String key) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        return stringRedisTemplate.hasKey(key);
    }

    /**
     * 获取缓存值
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 删除键
     *
     * @param key
     */
    public static void del(String key) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        stringRedisTemplate.delete(key);
    }

    /**
     * 删除所有的键值对
     */
    public static void delAll() {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        Set<String> keys = stringRedisTemplate.keys("*");
        stringRedisTemplate.delete(keys);
    }

    /**
     * 模糊删除
     *
     * @param key
     */
    public static void matchDelete(String key) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        stringRedisTemplate.delete(stringRedisTemplate.keys(key + "*"));
    }

    /**
     * 设置锁和超时时间
     *
     * @param lockKey
     * @param expireTime
     * @return
     */
    public static Boolean setNxAndExpireTime(String lockKey, long expireTime) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        Boolean aBoolean = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, lockKey, expireTime, TimeUnit.MINUTES);
        log.info("获取客户管理分布式锁{}, key = {}", aBoolean, lockKey);
        return aBoolean;
        /*Boolean aBoolean = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, lockKey);
        if (aBoolean) {
            stringRedisTemplate.expire(lockKey, expireTime, TimeUnit.MINUTES);
        }
        return aBoolean;*/
    }

    /**
     * 设置锁和超时时间
     * 注意: 这个方法并没有阻塞其他线程, 其他线程没有获取到锁则不会执行相应的代码段(不会等待)
     *
     * @param lockKey key
     * @param expireTime 过期时间
     * @param timeUnit 时间单位
     * @return boolean
     */
    public static Boolean setNxAndExpireTime(String lockKey, long expireTime, TimeUnit timeUnit) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        Boolean aBoolean = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, lockKey, expireTime, timeUnit);
        log.info("获取客户管理分布式锁{}", aBoolean);
        return aBoolean;
    }


    public static Long setList(String key, List<String> records) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        return stringRedisTemplate.opsForList().rightPushAll(key, records);
    }

    public static List<String> getList(String key) {
        StringRedisTemplate stringRedisTemplate = SpringContextUtil.getBean(StringRedisTemplate.class);
        return stringRedisTemplate.opsForList().range(key, 0, -1);
    }
}
